package com.top.zk.pandachannel;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.top.zk.pandachannel.fragment.HomeFragment;
import com.top.zk.pandachannel.fragment.LiveChinaFragment;
import com.top.zk.pandachannel.fragment.PandaCultureFragment;
import com.top.zk.pandachannel.fragment.PandaEyeFragment;
import com.top.zk.pandachannel.fragment.PandaLiveFragment;

public class MainActivity extends AppCompatActivity {
    private FragmentManager manager;
    private FragmentTransaction ft;
    private HomeFragment homeFragment;
    private PandaEyeFragment eyeFragment;
    private PandaCultureFragment cultureFragment;
    private PandaLiveFragment liveFragment;
    private LiveChinaFragment chinaFragment;
    private RadioGroup mRg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {
        //碎片管理者对象
        manager=getSupportFragmentManager();
        //碎片任务操作对象
        ft=manager.beginTransaction();
        //初始化碎片
        homeFragment=new HomeFragment();
        eyeFragment=new PandaEyeFragment();
        cultureFragment =new PandaCultureFragment();
        liveFragment=new PandaLiveFragment();
        chinaFragment = new LiveChinaFragment();

        ft.add(R.id.frame_layout_main_fl,homeFragment);
        ft.commit();

        mRg= (RadioGroup) findViewById(R.id.radiogroup_main_rg);
        mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //老忘记添加
                ft = manager.beginTransaction();
                switch (checkedId){
                    case R.id.radio_main_home:
                        if(!homeFragment.isAdded()){
                            ft.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out);
                            ft.replace(R.id.frame_layout_main_fl,homeFragment);
                        }
                        break;
                    case R.id.radio_main_sale:
                        ft.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out);
                        ft.replace(R.id.frame_layout_main_fl,eyeFragment);
                        break;
                    case R.id.radio_main_ten:
                        ft.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out);
                        ft.replace(R.id.frame_layout_main_fl,cultureFragment);
                        break;
                    case R.id.radio_main_cart:
                        ft.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out);
                        ft.replace(R.id.frame_layout_main_fl,liveFragment);
                        break;
                    case R.id.radio_main_account:
                        ft.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out);
                        ft.replace(R.id.frame_layout_main_fl,chinaFragment);
                        break;
                }
                ft.commit();
            }
        });
    }
}
